package org.micro.rbac.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.micro.rbac.core.constant.Global;

import java.nio.charset.StandardCharsets;

/**
 * FastJsonUtil
 *
 * @author hongsir
 * @since 2021/11/16 15:16
 */
public class FastJsonUtil {

    public static void print(Object object) {
        print(object, true);
    }

    public static void print(Object object, boolean format) {
        if (format) {
            String pretty = JSON.toJSONString(object, config().getWriterFilters(), JSONWriter.Feature.PrettyFormat);
            System.out.println(pretty);
        } else {
            System.out.println(object);
        }
    }

    public static FastJsonConfig config() {
        // 1. 配置fastjson
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat(Global.DATETIME_FORMAT);
        config.setCharset(StandardCharsets.UTF_8);
        config.setWriterFeatures(
                JSONWriter.Feature.WriteNullListAsEmpty,
                JSONWriter.Feature.WriteEnumUsingToString,
                //json格式化
                JSONWriter.Feature.PrettyFormat,
                //输出map中value为null的数据
                JSONWriter.Feature.WriteMapNullValue,
                JSONWriter.Feature.PrettyFormat,
                JSONWriter.Feature.WriteNullStringAsEmpty,
                JSONWriter.Feature.WriteNullListAsEmpty,
                //对map进行排序
                JSONWriter.Feature.MapSortField
        );
        return config;
    }


}
